package org.ykh.gmall.web.all.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.ykh.gamll.feign.list.client.ListFeignClient;
import org.ykh.gmall.list.SearchParam;
import org.ykh.gmall.result.Result;
import sun.nio.cs.ext.MacArabic;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@SuppressWarnings("all")
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;


    /**
     * 商品搜索
     * @return
     */
    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model){

        //数据回显
        model.addAttribute("searchParam",searchParam);
        //远程调用
        Result<Map> search = listFeignClient.search(searchParam);
        //设置数据
        model.addAllAttributes(search.getData());
        //拼接请求路径
        String urlParam =this.buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //拼接品牌面包屑
        String trademarkParam=this.buildTrademarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam",trademarkParam);
        //拼接平台属性
        List<Map>propsParamList=this.buildPropsParamList(searchParam.getProps());

        model.addAttribute("propsParamList",propsParamList);

        //设置排序对象
        Map orderMap=this.buildMap(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);

        return "list/index";
    }

    /**
     * 设置排序对象
     * @param order
     * @return
     */
    private Map buildMap(String order) {
        Map map=new HashMap();
        //判断
        if(!StringUtils.isEmpty(order)){
            //order=1:asc
            String[] split = order.split(":");

            map.put("type",split[0]);
            map.put("sort",split[1]);
        }else{
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    /**
     * 拼接平台属性
     * @param props
     * @return
     */
    private List<Map> buildPropsParamList(String[] props) {
        //判断
        if(props!=null &&props.length>0){

            List<Map> mapList = Arrays.stream(props).map(prop -> {

                //创建map对象
                Map resultMap = new HashMap<>();
                //prop=24:128G:机身内存
                String[] split = prop.split(":");
                //判断
                if (split != null && split.length == 3) {
                    resultMap.put("attrName", split[2]);
                    resultMap.put("attrValue", split[1]);
                    resultMap.put("attrId", split[0]);
                }
                return resultMap;
            }).collect(Collectors.toList());

         return mapList;
        }


        return new ArrayList<>();
    }

    /**
     * 拼接品牌面包屑
     * @param trademark
     * @return
     * trademark=1:小米
     */
    private String buildTrademarkParam(String trademark) {

        //判断
        if(!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            return "品牌:"+split[1];

        }
        return "";
    }

    /**
     * 拼接url路径
     *
     * http://list.gmall.com/list.html?category1Id=2
     * http://list.gmall.com/list.html?keyword=%E6%89%8B%E6%9C%BA
     * @param searchParam
     * @return
     */
    private String buildUrlParam(SearchParam searchParam) {
        //创建拼接对象
        StringBuilder builder=new StringBuilder();
        //判断
        if(!StringUtils.isEmpty(searchParam.getKeyword())){

            builder.append("keyword=").append(searchParam.getKeyword());

        }
        //判断
        if(searchParam.getCategory1Id()!=null){

            builder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if(searchParam.getCategory2Id()!=null){

            builder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if(searchParam.getCategory3Id()!=null){

            builder.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //拼接品牌 trademark=3:华为
        String trademark = searchParam.getTrademark();
        //判断
        if(!StringUtils.isEmpty(trademark)){

            //判断
            if(builder.length()>0){

                builder.append("&trademark=").append(searchParam.getTrademark());
            }

        }

        //判断平台属性
        //&props=24:128G:机身内存&props=107:1200mAh以下:电池容量&props=23:8G:运行内存&props=106:安卓手机:手机一级&order=
        String[] props = searchParam.getProps();
        //判断
        if(props!=null&&props.length>0){
            //判断
            if(builder.length()>0){
                for (String prop : props) {

                    builder.append("&props=").append(prop);
                }


            }

        }


        return "/list.html?"+builder.toString();
    }

//    public static void main(String[] args) {
//
//
//
//        //将map转换成实体类
//        Map<String,Object> map=new HashMap<>();
//        map.put("id",1l);
//        map.put("name","李四");
//        map.put("age",18);
//        //转换成json
//        String str = JSONObject.toJSONString(map);
//
//        User user = JSONObject.parseObject(str, User.class);
//        System.out.println(user);
//
//
//        //创建user对象
////        User user=new User();
////        user.setId(1l);
////        user.setName("张三");
////        user.setAge(11);
////        //将对象转换成map
////        String str = JSONObject.toJSONString(user);
////        //将string转换成map
////        Map map = JSONObject.parseObject(str, Map.class);
////        System.out.println(map);
//
//
//    }
}
